package cn.com.cetccst.tools.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;

import cn.com.cetccst.tools.log.CommonLogger;

public class Connectivity {

    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkCapabilities capabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
            if (capabilities == null) {
                CommonLogger.info("the active network is null");
                return false;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                CommonLogger.info("the active network  is mobiledata ");
                return true;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                CommonLogger.info("the active network  is wifi ");
                return true;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
                CommonLogger.info("the active network  is vpn ");
                return true;
            }
            CommonLogger.info("the active network  is unknown ");

        } else {
            CommonLogger.info("network manager is null");
        }
        return false;
    }

    public static boolean isMobileDataConnected(Context context) {
        ConnectivityManager manager = (ConnectivityManager)
                context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkCapabilities capabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
        if (capabilities != null) {
            return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
        }
        return false;
    }
}
